package com.ruoyi.delivery.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.delivery.domain.DelVehicle;
import com.ruoyi.delivery.domain.dto.Monitor;
import com.ruoyi.delivery.service.IDelVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/delivery/vehicle")
public class DelVehicleController {

    @Autowired
    private IDelVehicleService vehicleService;

    /**
     * 获取车辆信息列表，支持分页和筛选
     *
     * @param vehicleNo    车辆编号
     * @param vehiclePerson 车辆负责人
     * @param vehicleType   车辆类型
     * @param capacity      容量
     * @param startDate     创建时间开始范围
     * @param endDate       创建时间结束范围
     * @param activeStatus  生效状态
     * @param page          页码
     * @param pageSize      每页记录数
     * @return 分页车辆信息列表
     */
    @GetMapping("/list")
    public Map<String, Object> getVehicleList(
            @RequestParam(required = false) String vehicleNo,
            @RequestParam(required = false) String vehiclePerson,
            @RequestParam(required = false) String vehicleType,
            @RequestParam(required = false) Double capacity,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Boolean activeStatus,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        String logisticsDeliver = String.valueOf(SecurityUtils.getUserId());
        List<DelVehicle> vehicles = vehicleService.getVehicleList(vehicleNo,vehiclePerson, vehicleType, capacity, startDate, endDate, activeStatus, page, pageSize, logisticsDeliver);
        int total = vehicleService.getVehicleCount(vehicleNo,vehiclePerson, vehicleType, capacity, startDate, endDate, activeStatus, logisticsDeliver);

        // 打印
        System.out.println("车辆信息列表：");
        for (DelVehicle vehicle : vehicles) {
            System.out.println(vehicle.toString());
        }
        Map<String, Object> response = new HashMap<>();
        response.put("data", vehicles);
        response.put("total", total);
        response.put("page", page);
        response.put("pageSize", pageSize);
        return response;
    }

    /**
     * 新建车辆信息
     *
     * @param vehicle 车辆信息对象
     * @return 操作成功消息
     */
    @PostMapping
    public Map<String, String> insertVehicleInfo(@RequestBody DelVehicle vehicle) {
        vehicle.setUpdatedPerson("admin");
        vehicle.setLogisticsDeliver("1");
        vehicleService.insertVehicleInfo(vehicle);
        Map<String, String> response = new HashMap<>();
        response.put("message", "新建车辆信息成功");
        return response;
    }

    /**
     * 更新车辆信息
     *
     * @param vehicle 车辆信息对象
     * @return 操作成功消息
     */
    @PutMapping
    public Map<String, String> updateVehicleInfo(@RequestBody DelVehicle vehicle) {
        vehicleService.updateVehicleInfo(vehicle);
        Map<String, String> response = new HashMap<>();
        response.put("message", "更新车辆信息成功");
        return response;
    }

    /**
     * 删除车辆信息
     *
     * @param vehicleNo 车辆编号
     * @return 操作成功消息
     */
    @DeleteMapping("/delete/{vehicleNo}")
    public Map<String, String> deleteVehicle(@PathVariable String vehicleNo) {
        vehicleService.deleteVehicle(vehicleNo);
        Map<String, String> response = new HashMap<>();
        response.put("message", "删除车辆信息成功");
        return response;
    }

    /**
     * 批量删除车辆信息
     *
     * @param vehicleNos 车辆编号列表
     * @return 操作成功消息
     */
    @PostMapping("/batchDelete")
    public Map<String, String> deleteVehicles(@RequestBody List<String> vehicleNos) {
        vehicleService.deleteVehicles(vehicleNos);
        Map<String, String> response = new HashMap<>();
        response.put("message", "批量删除车辆信息成功");
        return response;
    }

    /**
     * 获取车辆位置信息
     *
     * @return 车辆位置信息
     */
    @GetMapping("/location")
    public Map<String, Object> getVehicleLocation() {
        String logisticsDeliver = String.valueOf(SecurityUtils.getUserId());
        List<Monitor> monitorVos = vehicleService.getVehicleLocation(logisticsDeliver);
        Map<String, Object> response = new HashMap<>();
        response.put("data", monitorVos);
        return response;
    }

    /**
     * 获取全部车辆信息
     *
     * @return 车辆信息
     */
    @GetMapping("/all")
    public Map<String, Object> getVehicleInfo() {
        String logisticsDeliver = String.valueOf(SecurityUtils.getUserId());
        List<DelVehicle> vehicles = vehicleService.getVehicleInfo(logisticsDeliver);
        Map<String, Object> response = new HashMap<>();
        response.put("data", vehicles);
        return response;
    }
}
